/*     */ package hmc;
/*     */ 
/*     */ import java.awt.Color;
/*     */ import java.util.ArrayList;
/*     */ import javax.swing.JTextPane;
/*     */ import org.python.core.PyObject;
/*     */ import org.python.util.PythonInterpreter;
/*     */ 
/*     */ public class LibraryMethods
/*     */ {
/*  41 */   ArrayList<String> capturedTemp = new ArrayList();
/*  42 */   ArrayList<String> captured = new ArrayList();
/*     */   Communicator comm;
/*     */   SerialPortCommunicator SC;
/*     */   LibraryMethods LM;
/*     */   SSHCommunicator SSHC;
/*     */   int timeLeft;
/*     */   int errorCount;
/*  55 */   public boolean stopped = true;
/*  56 */   private boolean SSH = false;
/*  57 */   public boolean waitforSucceeded = false;
/*     */ 
/*     */   public LibraryMethods(Communicator x, SerialPortCommunicator y)
/*     */   {
/*  68 */     this.comm = x;
/*  69 */     this.SC = y;
/*  70 */     this.LM = this;
/*     */   }
/*     */ 
/*     */   public LibraryMethods(Communicator x, SSHCommunicator y)
/*     */   {
/*  80 */     this.comm = x;
/*  81 */     this.SSHC = y;
/*  82 */     this.LM = this;
/*  83 */     this.SSH = true;
/*     */   }
/*     */ 
/*     */   public void addError(String errorMsg)
/*     */   {
/*  89 */     if (this.stopped)
/*     */     {
/*  91 */       this.errorCount += 1;
/*  92 */       newLine();
/*  93 */       this.comm.LMPrinting("***   ERROR: " + errorMsg + " (ErrorCount=" + this.errorCount + ")\n");
/*  94 */       transmit("");
/*     */     }
/*     */   }
/*     */ 
/*     */   public boolean capturePassed(String item, int verbose)
/*     */   {
/* 107 */     if (this.stopped)
/*     */     {
/* 109 */       for (int x = 0; x < this.captured.size(); x++)
/*     */       {
/* 111 */         if (((String)this.captured.get(x)).toLowerCase().replace(" ", "").replace("\t", "").contains(item.toLowerCase().replace(" ", "").replace("\t", "")))
/* 112 */           return true;
/*     */       }
/*     */     }
/* 115 */     this.errorCount += 1;
/*     */ 
/* 117 */     return false;
/*     */   }
/*     */ 
/*     */   public boolean captureWaitfor(String command, String start_string, String end_string, int timeout, int lineout, int verbosity)
/*     */   {
/* 140 */     this.captured = new ArrayList();
/*     */ 
/* 142 */     if (this.stopped)
/*     */     {
/*     */       try
/*     */       {
/* 147 */         Thread wf = new Thread(command, start_string, end_string, timeout, lineout) {
/*     */           public void run() {
/* 149 */             LibraryMethods.this.transmit(this.val$command);
/* 150 */             LibraryMethods.this.comm.captureWaitfor(this.val$start_string, this.val$end_string, this.val$timeout, this.val$lineout, LibraryMethods.this.LM);
/* 151 */             String[] strArray = LibraryMethods.this.comm.recordingString.split("\n");
/* 152 */             for (int x = 0; x < strArray.length; x++)
/* 153 */               LibraryMethods.this.captured.add(strArray[x]);
/*     */           }
/*     */         };
/* 156 */         wf.start();
/* 157 */         wf.join();
/*     */       }
/*     */       catch (InterruptedException e)
/*     */       {
/*     */       }
/*     */ 
/*     */     }
/*     */ 
/* 302 */     return false;
/*     */   }
/*     */ 
/*     */   public void execute(String item)
/*     */   {
/*     */     PyObject newObj;
/* 311 */     if (this.stopped)
/*     */     {
/* 313 */       PythonInterpreter interp = new PythonInterpreter();
/* 314 */       String filename = item.replace(".py", "");
/* 315 */       interp.exec("from " + filename + " import main");
/* 316 */       PyObject pyObject = interp.get("main");
/* 317 */       Object[] objectargs = { this };
/* 318 */       newObj = pyObject._jcall(objectargs);
/*     */     }
/*     */   }
/*     */ 
/*     */   public ArrayList<String> getCaptured()
/*     */   {
/* 329 */     if (this.stopped)
/*     */     {
/* 331 */       return this.captured;
/*     */     }
/*     */ 
/* 334 */     return null;
/*     */   }
/*     */ 
/*     */   public int getErrors()
/*     */   {
/* 344 */     return this.errorCount;
/*     */   }
/*     */ 
/*     */   public void hault()
/*     */   {
/* 354 */     this.stopped = false;
/*     */   }
/*     */ 
/*     */   public void newLine()
/*     */   {
/* 361 */     if (this.stopped)
/*     */     {
/* 364 */       this.comm.append("\n");
/*     */     }
/*     */   }
/*     */ 
/*     */   public void printCaptured()
/*     */   {
/* 395 */     if (this.stopped)
/*     */     {
/* 397 */       for (int x = 0; x < this.captured.size(); x++)
/*     */       {
/* 399 */         this.comm.LMPrinting("\n" + (String)this.captured.get(x));
/*     */       }
/*     */     }
/*     */   }
/*     */ 
/*     */   public void printErrorSummary()
/*     */   {
/* 409 */     if (this.stopped)
/*     */     {
/* 411 */       if (this.errorCount > 0)
/* 412 */         this.comm.LMPrinting(" FAILED: " + this.errorCount + " errors.");
/*     */       else
/* 414 */         this.comm.LMPrinting(" PASSED.");
/*     */     }
/*     */   }
/*     */ 
/*     */   public void printMessage(String item, Color col)
/*     */   {
/* 424 */     if (this.stopped)
/*     */     {
/* 426 */       if (col == null)
/* 427 */         printMessage(item);
/*     */       else
/* 429 */         this.comm.CustomPrinting(item, col);
/*     */     }
/*     */   }
/*     */ 
/*     */   public void printMessage(String item)
/*     */   {
/* 439 */     if (this.stopped)
/*     */     {
/* 441 */       this.comm.LMPrinting(item);
/*     */     }
/*     */   }
/*     */ 
/*     */   public void resume()
/*     */   {
/* 450 */     this.stopped = true;
/*     */   }
/*     */ 
/*     */   public String retrieveLineValue(String item)
/*     */   {
/* 461 */     if (this.stopped)
/*     */     {
/* 463 */       for (int x = 0; x < this.captured.size(); x++)
/*     */       {
/* 465 */         String temp1 = ((String)this.captured.get(x)).replace(" ", "").replace("\t", "").trim();
/* 466 */         String temp2 = item.replace(" ", "").replace("\t", "").trim();
/* 467 */         if (temp1.contains(temp2)) {
/* 468 */           return temp1.replace(temp2, "");
/*     */         }
/*     */       }
/*     */     }
/* 472 */     return "";
/*     */   }
/*     */ 
/*     */   public String retrieveLine(String item)
/*     */   {
/* 482 */     if (this.stopped)
/*     */     {
/* 484 */       for (int x = 0; x < this.captured.size(); x++)
/*     */       {
/* 486 */         if (((String)this.captured.get(x)).toLowerCase().replace(" ", "").replace("\t", "").contains(item.toLowerCase().replace(" ", "").replace("\t", "")))
/*     */         {
/* 488 */           return (String)this.captured.get(x);
/*     */         }
/*     */       }
/*     */     }
/*     */ 
/* 493 */     return "";
/*     */   }
/*     */ 
/*     */   public void simpleTransmit(String command)
/*     */   {
/*     */     try
/*     */     {
/* 505 */       if (this.stopped)
/*     */       {
/* 507 */         if (this.SSH)
/*     */         {
/* 509 */           this.SSHC.sendCommand(command);
/*     */         }
/*     */         else
/*     */         {
/* 513 */           this.SC.sendCommand(command);
/*     */         }
/*     */       }
/*     */     }
/*     */     catch (Exception e)
/*     */     {
/* 519 */       this.errorCount += 1;
/*     */     }
/*     */   }
/*     */ 
/*     */   public void transmit(String command)
/*     */   {
/*     */     try
/*     */     {
/* 534 */       if (this.stopped)
/*     */       {
/* 536 */         if (this.SSH)
/*     */         {
/* 542 */           waitfor(">", 1000, 0, false);
/* 543 */           Thread.sleep(100L);
/*     */ 
/* 545 */           this.comm.waitforIndex = ((this.comm.ConsoleBox.getText().split("\n").length + 1) % 50);
/* 546 */           this.SSHC.sendCommand(command);
/*     */ 
/* 549 */           if (command.equals("exit"))
/*     */           {
/* 551 */             Thread.sleep(100L);
/* 552 */             this.SSHC.sendCommand("");
/*     */           }
/*     */ 
/*     */         }
/*     */         else
/*     */         {
/* 561 */           waitfor(">", 1000, 0, false);
/* 562 */           this.comm.transmitted = ((this.comm.waitforIndex + 1) % 50);
/* 563 */           Thread.sleep(100L);
/*     */ 
/* 566 */           this.SC.sendCommand(command);
/*     */ 
/* 570 */           if (command.equals("exit"))
/*     */           {
/* 572 */             Thread.sleep(100L);
/* 573 */             this.SC.sendCommand("");
/*     */           }
/*     */         }
/*     */       }
/*     */ 
/*     */     }
/*     */     catch (Exception e)
/*     */     {
/* 581 */       this.errorCount += 1;
/*     */     }
/*     */   }
/*     */ 
/*     */   public boolean waitfor(String item, int timeout)
/*     */   {
/* 589 */     return waitfor(item, timeout, 0, true);
/*     */   }
/*     */ 
/*     */   public boolean waitfor(String item, int timeout, int lineout)
/*     */   {
/* 596 */     return waitfor(item, timeout, lineout, true);
/*     */   }
/*     */ 
/*     */   public boolean waitfor(String item, int timeout, int lineout, boolean errorCheck)
/*     */   {
/* 608 */     if (this.stopped)
/*     */     {
/*     */       try
/*     */       {
/* 613 */         Thread wf = new Thread(item, timeout, lineout) {
/*     */           public void run() {
/* 615 */             LibraryMethods.this.comm.waitfor(this.val$item, this.val$timeout, this.val$lineout, LibraryMethods.this.LM);
/*     */           }
/*     */         };
/* 618 */         wf.start();
/* 619 */         wf.join();
/*     */       }
/*     */       catch (InterruptedException e)
/*     */       {
/*     */       }
/*     */ 
/* 625 */       if ((errorCheck) && (!this.comm.waitforSucceeded))
/*     */       {
/* 627 */         this.errorCount += 1;
/* 628 */         newLine();
/* 629 */         this.comm.LMPrinting("***   ERROR: Failed to find string '" + item + "' (ErrorCount=" + this.errorCount + ")\n");
/* 630 */         transmit("");
/* 631 */         this.waitforSucceeded = false;
/*     */       }
/*     */       else {
/* 634 */         this.waitforSucceeded = true;
/*     */       }
/*     */ 
/*     */     }
/*     */ 
/* 752 */     return this.waitforSucceeded;
/*     */   }
/*     */ }

/* Location:           F:\6-30-2012\Hazell\
 * Qualified Name:     hmc.LibraryMethods
 * JD-Core Version:    0.6.0
 */